import xlrd
import xlwt

from PyQt5.QtWidgets import QMainWindow, QApplication, QTableWidgetItem, QFileDialog
import os
from main_ui import Ui_MainWindow
import sys


class MainApp(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainApp, self).__init__(parent)
        self.setupUi(self)
        self.query_btn.clicked.connect(self.read_excel)
        self.select_btn.clicked.connect(self.slot_btn_chooseFile)
        self.cwd = os.getcwd()
        self.actionlead_bar.triggered.connect(self.write_excel)
        self.jia_dicts = {}

    def read_excel(self):

        file_path = self.lineEdit.text()

        try:
            workbook = xlrd.open_workbook(file_path)
            # 获取所有sheet
            # print(workbook.sheet_names())
            sheet = workbook.sheet_by_index(0)
            rows = sheet.nrows  # 行
            cols = sheet.ncols  # 列
        except Exception as e:
            print('数据源错误！')
        else:
            gongxiu_dicts = {}
            temps_dicts = {}
            jia_type = self.select_combox.currentText()
            for i in range(0, rows):
                n = 0

                for t in range(0, cols):
                    if sheet.row(i)[t].value == jia_type:
                        # print(sheet.row(i)[3].value)
                        name = sheet.row(i)[3].value
                        n += 1
                        # gongxiu_dicts[name] = n
                        ks_name = sheet.row(i)[5].value
                        temps_dicts[name] = [n, ks_name]
                # print(temps_dicts)
                self.show_table.setRowCount(len(temps_dicts))
                j = 0
                for key, value in temps_dicts.items():
                    # print(key + ' 公休:' + str(value) + '天')
                    i = 0
                    self.show_table.setItem(j, i, QTableWidgetItem(key))
                    self.show_table.setItem(j, i+1, QTableWidgetItem(str(value[0])))
                    self.show_table.setItem(j, i + 2, QTableWidgetItem(value[1]))
                    j += 1
            if jia_type == '公休':
                self.jia_dicts.update(公休=temps_dicts)
            elif jia_type == '事假':
                self.jia_dicts.update(事假=temps_dicts)
            elif jia_type == '婚假':
                self.jia_dicts.update(婚假=temps_dicts)
            elif jia_type == '产假':
                self.jia_dicts.update(产假=temps_dicts)
            elif jia_type == '工伤':
                self.jia_dicts.update(工伤假=temps_dicts)
            elif jia_type == '病假':
                self.jia_dicts.update(病假=temps_dicts)
            elif jia_type == '探亲':
                self.jia_dicts.update(探亲假=temps_dicts)
            print(self.jia_dicts)

    def write_excel(self):

        fileName, file_type = QFileDialog.getSaveFileName(self, "文件保存", "/", "文件 (*.xls);")

        if fileName == "":
            return
        try:
            workbook = xlwt.Workbook()

            for key, value in self.jia_dicts.items():
                worksheet = workbook.add_sheet(key)
                i = 0
                for name, values in value.items():
                    j = 0
                    worksheet.write(i, j, name)
                    worksheet.write(i, j+1, str(values[0]))
                    worksheet.write(i, j+2, values[1])
                    i += 1

            workbook.save(fileName)
        except Exception as e:
            print(e)

    def slot_btn_chooseFile(self):
        fileName_choose, filetype = QFileDialog.getOpenFileName(self,
                                                                "选取文件",
                                                                self.cwd,  # 起始路径
                                                                "Text Files (*.xls);;All Files (*)")  # 设置文件扩展名过滤,用双分号间隔

        if fileName_choose == "":

            return

        self.lineEdit.setText(fileName_choose)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    myWin = MainApp()
    myWin.show()
    sys.exit(app.exec_())